@using System.Linq;
@using TfsCloudDeploy.Deployment.Website.ViewModels;
<!DOCTYPE html>
<html lang="en">
@{
    // Initialize server-side objects that will be used on the page here.
    // These can be parts of a view-model or objects from the ViewBag.
    
    var applicationPath = Url.Content("~/");

    bool isPackageView = (((string)ViewBag.PackageName).ToLowerInvariant().CompareTo("index") != 0);

    var normalizedModel = ((IEnumerable<DeploymentViewModel>)(Model.Deployments)).Select(
        m => new
        {
            Name = m.DeploymentName,
            CssClass = m.CssClass,
            Link = m.Link,
            NS = m.NumSucceeded,
            NI = m.NumInProgress,
            NF = m.NumFailed
        });
    var initialDeploymentData = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(normalizedModel);
    var packageName = (ViewBag.PackageName == "") ? "Index" : ViewBag.PackageName;
    var initialDeploymentBuildTargetHostsData = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ViewBag.DeploymentBuildTargetHosts);
    string basePageTitle = string.Format("{0} - {1}", ViewBag.ReleaseEnvironment, ViewBag.Title);
}
<head>
    <meta charset="utf-8" />
    <title>@basePageTitle</title>
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <link href="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Content/themes/base/css")" rel="stylesheet" type="text/css" />
    <link href="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Content/css")" rel="stylesheet" type="text/css" />
    <script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Scripts/js")"></script>
    <script type="text/javascript" src="@Url.Content("~/Scripts/knockout.debug.js")"></script>
    <meta name="viewport" content="width=device-width" />
</head>
<body>
    <header>
            <div class="content-wrapper">
                <div class="site-title-container">
                    <a href="~/" class="SiteLogo">SiteLogo</a>
                    <p class="site-title">@Html.ActionLink((string)ViewBag.SiteTitle, "Index", "Home", new RouteValueDictionary() { { "packageName", "Index" } }, null)<span class="beta">Beta</span></p>
                </div>
                <div class="float-right">
                    <section id="login">
                        <span class="username">@User.Identity.Name</span>
                    </section>
                    <section id="location">
                        <span class="locationname">Environment: <b>@ViewBag.ReleaseEnvironment</b></span>
                    </section>
                </div>
            </div>
    </header>
    <div id="body">
        @RenderSection("featured", required: false)
        <div class="float-left">
            <nav>
                    <ul id="menu">
                        <li><br /></li>
                        <li><br /></li>
                        <li>AVAILABLE PACKAGES:</li>
                        <li><br /></li>
                    </ul>
                    <ul id="menu" data-bind="foreach: deployments">
                        <li data-bind="attr: {id: Name}"><a data-bind="attr: {href: '@applicationPath' + Link, class: CssClass}">&nbsp;</a><a data-bind="attr: {href: '@applicationPath' + Link}"><span data-bind="text: Name, attr: { id: Name }" onmouseover="showStatusCount(this)" onmouseout="hideStatusCount(this)">&nbsp;</span></a><div class="status-count-box" data-bind="attr: { id: 'stats_' + Name }"><span class="num-succeeded-box" data-bind="text: NS"></span><span class="num-inprogress-box" data-bind="text: NI"></span><span class="num-failed-box" data-bind="text: NF"></span></div></li>
                    </ul>
                    <ul id="menu">
                        <li><br /></li>
                        <li><br /></li>
                    </ul>
                </nav>
        </div>
        <section class="content-wrapper main-content clear-fix">
                <section class="featured">
                    <div class="content-wrapper">
                        <hgroup class="title">
                            <h1>@ViewBag.Title.</h1>
                            <h2>@ViewBag.Message</h2>
                            <br /><br />
                                @if (isPackageView)
                                {
                                    <span class="package-box">Selected Package: <b>@ViewBag.PackageName</b></span>
                                }
                                else
                                {
                                    <h6>@ViewBag.Description</h6>
                                }
                        </hgroup>
                    </div>
                </section>

                <section class="features">
                    <div>
                        <nav>
                            <ul class="new-horiz-menu">
                            @if (!Model.IsTransportOnly)
                            { 
                                @*<li>@Html.RouteLink("Test", "PackageOperation", new { packageName = ViewBag.PackageName, action = "Test" })</li>
                                <li>@Html.RouteLink("Check Status", "PackageOperation", new { packageName = ViewBag.PackageName, action = "CheckStatus" })</li>
                                <li>@Html.RouteLink("Service Info", "PackageOperation", new { packageName = ViewBag.PackageName, action = "CheckInfo" })</li>*@
                                <li>@Html.RouteLink("Deploy", "PackageOperation", new { packageName = ViewBag.PackageName, action = "Deploy" })</li>
                                @*<li>@Html.RouteLink("Validate", "PackageOperation", new { packageName = ViewBag.PackageName, action = "Validate" })</li>
                                <li>@Html.RouteLink("Bring Online", "PackageOperation", new { packageName = ViewBag.PackageName, action = "BringOnline" })</li>
                                <li>@Html.RouteLink("Take Offline", "PackageOperation", new { packageName = ViewBag.PackageName, action = "TakeOffline" })</li>
                                <li>@Html.RouteLink("Restart", "PackageOperation", new { packageName = ViewBag.PackageName, action = "Restart" })</li>
                                <li>@Html.RouteLink("Backup", "PackageOperation", new { packageName = ViewBag.PackageName, action = "Backup" })</li>
                                <li>@Html.RouteLink("Rollback", "PackageOperation", new { packageName = ViewBag.PackageName, action = "Rollback" })</li>*@
                            }
                            else
                            {
                                <li>@Html.RouteLink("Transport", "PackageOperation", new { packageName = ViewBag.PackageName, action = "Transport" })</li>
                            }
                            </ul>
                        </nav>
                    </div>
                </section>
                <div class="operation-body">
                    @if (isPackageView)
                    { 
                    <section class="body-main-deployment-build">
                        <span class="build-box">Current Deployment: <b>@ViewBag.DeploymentBuildName</b></span>
                    </section>
                    }
                    <section class="body-main">
                        <div class="body-content-wrapper">
                            <table>
                                <thead>
                                    <td><input id="AllCheckBoxes" type="checkbox" /></td>
                                    <td>&nbsp;</td>
                                    <td>Host Name</td>
                                    <td>Service Status</td>
                                </thead>
                                <tbody data-bind="foreach: deploymentBuildTargetHosts">
                                    @if (isPackageView)
                                    { 
                                    <tr>
                                        <td><input class="hostCheckBoxes" data-bind="attr: {id: 'host_' + HostName, visible: CssClass != ''}, checked: getCheckedState('host_' + HostName)" type="checkbox" /></td>
                                        <td><a href="#" data-bind="attr: {class: CssClass}">&nbsp;</a></td>
                                        <td><a class="deployment-detail-dialog-href" href="#" data-bind="attr: {id: 'dialog_' + HostName}" onclick="openDeploymentDetailsDialog(this)"><span data-bind="text: HostName">&nbsp;</span></a></td>
                                        <td><a href="#" data-bind="attr: {text: SvcState}">&nbsp;</a></td>
                                    </tr>
                                    }
                                    else
                                    { 
                                    <tr>
                                        <td>&nbsp;</td>
                                        <td>&nbsp;</td>
                                        <td><span data-bind="text: HostName">&nbsp;</span></td>
                                        <td>&nbsp;</td>
                                    </tr>
                                    }
                                </tbody>
                            </table> 
                        </div>
                    </section>
                @RenderBody()
                </div>
            </section>
    </div>
    <footer>
            <div class="content-wrapper">
                <div class="float-left-footer">
                    <p>Last refreshed from server on: <span class="last-refreshed-caption" data-bind="text: currentDateTimeBinding"></span></p>
                    <p class="float-left-footer">&copy; @DateTime.Now.Year - TFS Cloud Deploy</p>
                </div>
            </div>
    </footer>
    <div id="deploymentDetailsDialog" title="Deployment Details: @ViewBag.DeploymentBuildName">Retrieving details...<br /><img src="~/Images/loader1.gif" alt="" /></div>
    @*<div id="deploymentDetailsDialog" title="Deployment Details: @ViewBag.DeploymentBuildName">Retrieving details...<br /><img src="~/Images/loader1.gif" alt="" /></div>*@
    <script type="text/javascript">
        var initialDeploymentData = @Html.Raw(initialDeploymentData);
        var initialDeploymentBuildTargetHostsData = @Html.Raw(initialDeploymentBuildTargetHostsData);
        var basePageTitle = "@basePageTitle";
        var pageTitle = basePageTitle;
        var packageName = "@packageName";
        var applicationPath = '@applicationPath';
        var checkboxData = [];
        var currentDateTime = Date();

        // Setup a timer to poll server for updates.
        var timerIntervalInMilliseconds = @ViewBag.PollingIntervalInMilliseconds;
    </script>
    <script type="text/javascript" src="@Url.Content("~/Scripts/deployment.js")"></script>
</body>
</html>
